package com.witfortos.kotlindemo.ui

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.witfortos.common.base.BaseActivity
import com.witfortos.common.widget.BaseDecoration
import com.witfortos.kotlindemo.R
import com.witfortos.kotlindemo.common.adp.NewsAdp
import com.witfortos.kotlindemo.mvvm.view_model.MainVm
import kotlinx.android.synthetic.main.act_main.*

class MainAct : BaseActivity<MainVm>() {
    override fun layoutId(): Int = R.layout.act_main;
    var pageNum: Int = 1;
    var newsAdp: NewsAdp? = null;

    override fun initView(savedInstanceState: Bundle?) {
        val lm = LinearLayoutManager(this)
        recyclerView.layoutManager = lm;
        recyclerView.addItemDecoration(BaseDecoration.create(Color.GRAY, 1))

        newsAdp = NewsAdp();
        recyclerView.adapter = newsAdp;
    }

    override fun initData() {
        viewModel.getNews(pageNum, 1);

        viewModel.news.observe(this, Observer { it ->
            newsAdp!!.addData(it.list);
        })
    }


    companion object {
        fun navigateToMainAct(context: Context) {
            val i = Intent(context, MainAct().javaClass);
            context.startActivity(i);
        }
    }

}